package com.ruoyi.system.amqp.taskscheduler;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.LineDevice;
import com.ruoyi.system.service.ILineDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCacheUpdateTask
{
    @Autowired
    RedisCache redisCache;

    @Autowired
    ILineDeviceService iLineDeviceService;

    public void setRedisCache(String name)
    {
        redisCache.setCacheObject(name, null);
        LineDevice lineDevice = iLineDeviceService.selectLineDeviceByName(name);
        redisCache.setCacheObject(name, lineDevice);
        System.out.println("Updated " + lineDevice.getClass().getName() + " Redis Cache: " + name);
    }
    public LineDevice getRedisCache(String deviceName)
    {
        LineDevice lineDevice = redisCache.getCacheObject(deviceName);
        lineDevice.setName(deviceName); //不能少！！！
        // redisCache.expire(deviceName, 30, TimeUnit.SECONDS); // 暂不设置缓存有效时间
        return lineDevice;
    }
    public void deleteAllDevicesRedisCache()
    {
        List<LineDevice> list= iLineDeviceService.selectLineDeviceList(new LineDevice());
        for (LineDevice lineDevice : list) {
            boolean isDeleted =  redisCache.deleteObject(lineDevice.getName());
            if(isDeleted)
                System.out.println
                        ("Deleted " + lineDevice.getClass().getName()
                                + " Redis Cache: " + lineDevice.getName());
        }
    }
}
